<?


require_once  $_SERVER['DOCUMENT_ROOT']."/../_lib/base/common.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.template.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_references.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holidays.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_filters.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.destinations.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.exchange_rates.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.hotels.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.airports.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.photos.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.user_auth.php";
require_once SITE_ROOT_PATH."/_lib/classes/class.holiday_special_offers.php";


	// See if user is logged in 
	$user_auth = new user_auth();
	$user_auth->user_auth_bookings();


	$template = new template();
	$template->head_booking();
	

// FUNCTION // Show home page
function display_home(){	
	
?>

<h2>Holiday Reference Lookup</h2>


<?
if($GLOBALS['note']){
	echo display_message($GLOBALS['note']);
}
?>


<form action="<?= $_SERVER['PHP_SELF']?>" method="get">

	<input type="hidden" name="action" value="search" />

	<table class="libra">
		<tr>
			<td width="150"><strong>Holiday reference:</strong></td>
			<td><input type="text" name="reference_id" value="<?= $_REQUEST['reference_id'] ?>"></td>
		</tr>
		<tr>
        <td></td>
			<td><input type="submit" value="submit" class="form_button"></td>
		</tr>
	</table>


</form>



<?
}
// END FUNCTION 














// FUNCTION // Display result
function display_result($values){


	$holidays = new holidays();
	$destinations = new destinations();
	$airports = new airports();
	$photos = new photos();
	$holiday_filters = new holiday_filters();
	$specials = new holiday_special_offers();

// Check if its a special offer
$params['reference_id'] = $values['reference_id'];
$special_values = $specials->get_special($params);

?>

<h2>Holiday Reference Lookup</h2>


<h3>Results</h3>

<!-- START LEFT COL -->


<div class="col_490 margin_right_20">



<div class="border_box">

<fieldset><legend>Holiday Details</legend></fieldset>

<table class="libra">
      <tr> 
        <th width="170">Reference number</th>
        <td><strong><?= $values['reference_id'] ?></strong></td>
      </tr>
     <tr> 
        <th>Tour operator</th>
        <td><?
		
		$params['tour_op_code'] = $values['tour_op'];
		$my_tour_op = $holidays->get_tour_operator($params);
		
		echo $values['tour_op']." (".$my_tour_op.")";
		
		?></td>
     </tr>
	 	  <tr> 
        <th>Holiday code</th>
        <td><?= $values['holiday_code'] ?></td>
      </tr>
	  	  <tr> 
        <th>Travel code</th>
        <td><?= $values['travel_code'] ?></td>
      </tr>
	  	  <tr> 
        <th>Flight out number</th>
        <td><?= $values['flight_out_number'] ?></td>
      </tr>
	  <tr> 
        <th>Departure date</th>
        <td><?= display_sabs_date($values['departure_date'],'d / m / Y') ?></td>
      </tr>
	  <tr> 
        <th>Flight Out Time</th>
        <td>Departs: <?= sabs_flight_time_2_std($values['depart_dept_time']); ?><br />
        Arrives: <?= sabs_flight_time_2_std($values['arrive_dest_time']); ?></td>
      </tr>
	  <tr> 
        <th>Return date</th>
        <td><?= display_sabs_date($values['return_date'],'d / m / Y') ?></td>
      </tr>
	  <tr> 
        <th>Return Flight Time</th>
        <td>Departs: <?= sabs_flight_time_2_std($values['depart_dest_time']); ?><br />
        Arrives: <?= sabs_flight_time_2_std($values['arrive_dept_time']); ?></td>
      </tr>
     <tr> 
        <th>Nights</th>
        <td><?= $values['nights'] ?></td>
	</tr>
      <tr> 
        <th>Departure airport</th>
        <td><? 
		
	// Departure airport lookup
	$params['airport_code'] = $values['departure_airport'];
	$departure_airport_values = $airports->get_airport($params);
	unset($params);

	echo $values['departure_airport']." (".$departure_airport_values['simple'].")";
		
		
		  ?></td>
      </tr>
	 <tr> 
        <th>Destination airport</th>
        <td><? 
		
		
	// Destination airport lookup
	$params['airport_code'] = $values['destination_airport'];
	$destination_airport_values = $airports->get_airport($params);
	unset($params);


	echo $values['destination_airport']." (".$destination_airport_values['simple'].")"; 
		
		
		?></td>
     </tr>
     
     
     
 <tr> 
        <th>Return airport</th>
        <td><? 
		
		
	// Return airport lookup
	$params['airport_code'] = $values['return_airport'];
	$return_airport_values = $airports->get_airport($params);
	unset($params);


	
		if($values['return_airport']==$values['departure_airport']){
		echo $values['return_airport']." (".$return_airport_values['simple'].")"; 
		}else{
		echo "<strong style=\"color:red\">".$values['return_airport']." (".$return_airport_values['simple'].")</strong>"; 
		}
		
		?></td>
     </tr>

	  <tr> 
        <th>Resort</th>
        <td><?= $values['resort'] ?></td>
	</tr>
	  <tr> 
        <th>Accommodation</th>
        <td><?= $values['hotel_name'] ?></td>
      </tr>
	  <tr> 
        <th>Board basis</th>
        <td><?= $values['board_basis'] ?></td>
      </tr>
	  <tr> 
        <th>Base - Adult price</th>
        
<?


	
	// Get the BASE price based on the departure airport and exchange rates
	// Adult Price
	$params['price'] = $values['price_adult'];
	$params['departure_airport'] = $values['departure_airport'];
	$params['symbol'] = true;
	$display_price = $holidays->display_price($params);
	$params['tour_op'] = $values['tour_op'];
	$display_price_adult = $holidays->display_price($params);
	unset($params);
	
	
	// Child Price
	if(trim(floor($values['price_child']))){
		$params['price'] = $values['price_child'];
		$params['departure_airport'] = $values['departure_airport'];
		$params['symbol'] = true;
		$display_price = $holidays->display_price($params);
		$params['tour_op'] = $values['tour_op'];
		$display_price_child = $holidays->display_price($params);
		unset($params);
	}
	
	
	$effective_price_adult = substr($display_price_adult,6);
	$effective_price_child = substr($display_price_child,6);

	// See if the price has been filtered
	$mod_price = $holiday_filters->find_holiday_mod_price($values['reference_id'],true);

	if($mod_price){
		
		$effective_price_adult = $mod_price['mod_price'];
		if($mod_price['price_adult']) $effective_price_adult = $mod_price['price_adult'];
		if($mod_price['price_child']) $effective_price_child = $mod_price['price_child'];
		
		$effective_price_suffix = " (Price updated ".date('jS M Y', $mod_price['entry_date']).")";
		
	}


	
if($my_supliment = $holidays->get_suppliment_price($values['tour_op'])){
	$sup_highlight = " class='red' ";
}
?> 

        <td><?= $display_price_adult ?></td>
      </tr>
<tr> 
<th <?= $sup_highlight ?>>Display - Adult price</th>
<td <?= $sup_highlight ?>>&euro;<?= $effective_price_adult.$effective_price_suffix; ?></td>
</tr>
<? 
if(trim(floor($values['price_child']))){ 
?>
	  <tr> 
        <th>Base - Child price</th>
        <td><?= $display_price_child ?></td>
      </tr>
	  <tr> 
        <th <?= $sup_highlight ?>>Display - Child price </th>
        <td<?= $sup_highlight ?>>&euro;<?= $effective_price_child ?></td>
      </tr>
	  <? } ?>
      
</table>
<br />

<!--
these are not stored in the holiday string



<fieldset><legend>Passengers</legend></fieldset>

<table class="libra">
      <tr> 
        <th width="170">PAX adult</th>
        <td><?= floor($values['pax_adult']) ?></td>
      </tr>
	  <tr> 
        <th>PAX child</th>
        <td><?= floor($values['pax_child']) ?></td>
      </tr>
	  <tr> 
        <th>PAX infant</th>
        <td><?= floor($values['pax_infant']) ?></td>
      </tr>
</table>
-->

</div>




<br />


<div class="message_pos message">

	<p class="terms" style="margin: 0 ">* Price as displayed to the customer on Latedeals.ie<br/>
	The following tour operators have set price increase:<br/>
	<ul class="terms">
		<li><strong>Falcon + &euro;<?=	$holidays->get_suppliment_price('FAL'); ?></strong></li>
		<li><strong>Stien + &euro;<?=	$holidays->get_suppliment_price('STE'); ?></strong></li>
		<li><strong>Sunway + &euro;<?=	$holidays->get_suppliment_price('SWY'); ?></strong></li>
		<li><strong>Topflight + &euro;<?=	$holidays->get_suppliment_price('TFL'); ?>  </strong></li>
		<li><strong>Sunworld + &euro;<?=	$holidays->get_suppliment_price('SWI'); ?>  </strong></li>
		<li><strong>Abbey Travel + &euro;<?=	$holidays->get_suppliment_price('ABY'); ?>  </strong></li>
		<li><strong>Panorama + &euro;<?=	$holidays->get_suppliment_price('PAN'); ?>  </strong></li>
	</ul>
	</p>

</div>

<br />


<div class="message_pos message">
<?

	$exchange_rates = new exchange_rates();
	$ex_rate = $exchange_rates->get_exchange_rate('GBP',true);

?>
	<p>Exchange rate: &euro;1 =  &pound;<?= $ex_rate ?></p>

</div>

<br />

<?


if(!empty($special_values)){

?>

<div class="message_pos message">

	<p>This holiday offer was uploaded as a <strong>Special Offer</strong>. To edit or remove this offer please go to <a href="/holiday_specials.php">Special Offer > Holiday Specials</a></p>

</div>

<? }else{ ?>


<div class="border_box">



<? if(!empty($mod_price)){ ?>

<fieldset><legend>Holiday Filter History</legend></fieldset>

<p>The base price of this holiday was updated from it's orignal base price of <strong>&euro;<?= floor($values['price_adult']) ?></strong> to <strong>&euro;<?= $effective_price_adult ?></strong>.</p>


<? } ?>








<fieldset><legend>Holiday Filter</legend></fieldset>




<script>
$(document).ready(function(){

	required = ["filter_holiday", "filter_flight", "mod_price_adult", "mod_price_child"];

	$("#filter_form").submit(function(){
									  
		var valid = false;
		
		for (i=0;i<required.length;i++) {
			var input = $('#'+required[i]);
			
			if((input.attr('type')) == "checkbox"){
				if(input.attr("checked") == "checked") valid = i;
			}
			
			if((input.attr('type')) == "text"){
				if (input.val() !== "")  valid = i;
			}
		}
		
		if (valid) {
			return true;
		} else {
			return false;
			alert('Please fill out form');
		}
		
	});
	
});	


</script>


<form action="filter.php" id="filter_form"  method="get">

    <input type="hidden" name="reference_id" value="<?= $values['reference_id'] ?>" />
    <input type="hidden" name="action" value="filter_confirm" />
    <input type="hidden" name="tour_op" value="<?= $values['tour_op'] ?>" />

<table class="libra">
      <tr> 
        <td width="170"><label>Remove this holiday</label></td>
            <td><input type="checkbox" name="filter_holiday" id="filter_holiday" /></td>
        </tr>
        <tr> 
            <td><label>Remove the entire flight</label></td>
            <td><input type="checkbox" name="filter_flight" id="filter_flight" /></td>
        </tr>
        <tr> 
            <td colspan="2"><label>Modify the <strong>Display Price</strong></label></td>
        </tr>
        <tr> 
            <td><label>Adult price</label></td>
            <td>&euro;<input type="text" name="mod_price_adult" id="mod_price_adult" value="<?= $effective_price_adult ?>" style="width: 50px;" /> <span  class='red' >+ &euro;<?= $holidays->get_suppliment_price($values['tour_op']); ?> set suppliment</span></td>
        </tr>
        <tr> 
            <td><label>Child price</label></td>
            <td>&euro;<input type="text" name="mod_price_child" id="mod_price_child" value="<?= $effective_price_child ?>" style="width: 50px;" /> <span  class='red' >+ &euro;<?= $holidays->get_suppliment_price($values['tour_op']); ?> set suppliment</span></td>
        </tr>
    </table>
    
    
    <input type="submit" value="submit"/>

</form>

</div>



<br />



<div class="border_box">

<fieldset><legend>Generate Request & Response Messages</legend></fieldset>



<p>Output the SABS Request & Response Messages to include in bug reports.</p>

<form action="request_response.php" method="get">

<input type="hidden" value="<?= $values['reference_id'] ?>" name="reference_id" />

<input type="submit" value="submit"/>

</form>

</div>




<? } ?>





  
</div>

<!-- END LEFT COL -->












<!-- START RIGHT COL -->

<div class="col_490">










<!-- Start holiday preview -->


<div class="border_box">

<fieldset><legend>Hotel Details</legend></fieldset>




<?

	$hotels = new hotels();
	$tree = new tree();

	$params = array();
    $params['supplier_code'] = 'mcs';
    $params['tour_op'] = $values['tour_op'];
	$params['hotel_name'] = $values['hotel_name'];
	$params['destination_airport'] = $values['destination_airport'];
	$params['resort'] = $values['resort'];
	$params['travel_code'] = $values['travel_code'];
	$params['resort_code'] = $values['resort_code'];
	$params['holiday_code'] = $values['holiday_code'];
	$params['hotel_code'] = $values['hotel_code'];
	// Check if its a special offer
	if(!empty($special_values['hotel_tree_id'])){
		$params['tree_id'] = $special_values['hotel_tree_id'];
	}
	$lookup = $hotels->lookup_hotel($params);
	unset($params);
	
	// Set values
	$hotel_values = $lookup['hotel_values'];
	$resort_values = $lookup['resort_values'];
	$destination_values = $lookup['destination_values'];
	
	// Get photo url
	$lead_hotel_thumb_url = $tree->photo_url($hotel_values['main_photo']);

	

	if($hotel_values){



?>

	



<h5><?= $hotel_values['name'].", ".$resort_values['name'].", ".$destination_values['name'] ?></h5>





<img class="photo_right" src="<?= $lead_hotel_thumb_url ?>" />




<p><?= nl2br($hotel_values['description']) ?></p>



<!-- END ACCOMMODATION -->
	
    
    
    
	

	
<!-- START RESORT -->

<h6><?= $resort_values['name'] ?></h6>

<p><?= nl2br($resort_values['description']) ?></p>


<!-- START destination -->
 
<h6 title="<?= $destination_values['name'] ?>"><?= $destination_values['name'] ?></h6>

<p><?= nl2br($destination_values['description']) ?></p>





<? }else{ ?>

<h5>No hotel description available</h5>




<? } ?>


</div>





</div>

<!-- END RIGHT COL -->



<?


}
// END FUNCTION






switch ($_REQUEST['action']){
	default:
		display_home();
	break;
	case "search":
		$holiday_references = new holiday_references();

		$holiday_values = $holiday_references->get_holiday($_GET['reference_id']);
		$holiday_values['reference_id'] = $_GET['reference_id']; 
		
		// No reference given
		if(empty($_GET['reference_id'])){
			$my_message = array();
			$my_message['type'] = 'neg';
			$my_message['message'] = "Please enter a reference";
			$GLOBALS['note'] = $my_message;	
			$error = true;	
		}elseif(!$holiday_values){
			$my_message = array();
			$my_message['type'] = 'neg';
			$my_message['message'] = "Reference not found in the system";
			$GLOBALS['note'] = $my_message;
			$error = true;	
		}
		
		
		if($error){
			display_home();
		}else{
			display_result($holiday_values);
		}
		
	break;
	
}


	$template->foot_booking();
	

?>